package com.lottery.shadowbd.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lottery.shadowbd.R;
import com.lottery.shadowbd.utils.SharedPreUtil;
import com.lottery.shadowbd.utils.SizeConvertUtil;

import java.util.ArrayList;
import java.util.List;

import static com.lottery.shadowbd.support.Constant.isFirst;

public class GuideActivity extends AppCompatActivity {

    private ViewPager vp_guide;
    private RadioGroup rg_dot;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        vp_guide = (ViewPager) findViewById(R.id.vp_guide);
        rg_dot = (RadioGroup) findViewById(R.id.rg_dot);

        initData();
    }

    private void initData() {
        mContext = this;

        List<View> mList = new ArrayList<View>();

        ImageView iv = new ImageView(mContext);
        iv.setBackgroundResource(R.mipmap.guide1);
        mList.add(iv);

        iv = new ImageView(mContext);
        iv.setBackgroundResource(R.mipmap.guide2);
        mList.add(iv);

        iv = new ImageView(mContext);
        iv.setBackgroundResource(R.mipmap.guide3);
        mList.add(iv);

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
        FrameLayout fLayout = new FrameLayout(mContext);

        iv = new ImageView(mContext);
        iv.setBackgroundResource(R.mipmap.guide4);

        int dp10 = SizeConvertUtil.dp2px(mContext, 10);
        int dp5 = dp10 / 2;
        int dp15 = dp5 * 3;

        TextView tvBegin = new TextView(mContext);
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        params.bottomMargin = dp10 * 4;

        tvBegin.setBackgroundResource(R.drawable.shape_now_go);
        tvBegin.setText(R.string.tClickToNext);
        tvBegin.setTextSize(14);
        tvBegin.setTextColor(ContextCompat.getColor(mContext, R.color.white));
        tvBegin.setPadding(dp15, dp5, dp15, dp5);
        tvBegin.setLayoutParams(params);

        fLayout.removeAllViews();
        fLayout.addView(iv);
        fLayout.addView(tvBegin);
        mList.add(fLayout);

        initPager(mList, dp5);

        tvBegin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreUtil.getInstance(mContext).putString(isFirst, "1");

                Intent mIntent = new Intent(mContext, HomeActivity.class);

                startActivity(mIntent);

                finish();
            }
        });
    }

    private void initPager(List<View> mList, int dp5) {
        StartPagerAdapter adapter = new StartPagerAdapter(mList);
        vp_guide.setAdapter(adapter);
        rg_dot.removeAllViews();

        RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(SizeConvertUtil.dip2px(mContext,16), SizeConvertUtil.dip2px(mContext,8));
        params.leftMargin = dp5;
        params.rightMargin = dp5;

        for (int i = 0; i < mList.size(); i++) {
            RadioButton rb = new RadioButton(mContext);
            //rb.setPadding(dp5, 0, dp5, 0);
            rg_dot.addView(rb,params);
            rb.setButtonDrawable(R.drawable.selector_dot);
            if (i == 0) {
                rb.setChecked(true);
            }
        }
        vp_guide.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                ((RadioButton) rg_dot.getChildAt(arg0)).setChecked(true);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
                switch (arg0) {
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        break;
                    case ViewPager.SCROLL_STATE_SETTLING:
                        break;
                    case ViewPager.SCROLL_STATE_IDLE:
                        /*if (vp_guide.getCurrentItem() == vp_guide.getAdapter().getCount() - 1
                                && !flag) {
                            SharedPreUtil.getInstance(mContext).putString(Constant.IsFirst,"1");
                            //用户id
                            String uid = SharedPreUtil.getInstance(mContext).getString(Constant.UserId,"");

                            if(!"".equals(uid)) {
                                String identity = SharedPreUtil.getInstance(mContext).getString(Constant.UserIdentity,"");

                                if("1".equals(identity)) {//用户
                                    startActivity(new Intent(mContext, MainUserActivity.class));
                                } else if("2".equals(identity)) {//律师
                                    startActivity(new Intent(mContext, MainLawyerActivity.class));
                                } else {//身份信息异常
                                    startActivity(new Intent(mContext, LoginActivity.class));
                                }
                            } else {
                                startActivity(new Intent(mContext, LoginActivity.class));
                            }

                            finish();
                        }*/
                        break;
                }
            }
        });
    }

    class StartPagerAdapter extends PagerAdapter {

        private List<View> mList;

        public StartPagerAdapter(List<View> list) {
            mList = list;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        /*
         * (non-Javadoc)
         *
         * @see
         * android.support.v4.view.PagerAdapter#destroyItem(android.view.ViewGroup
         * , int, java.lang.Object)
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            container.removeView(mList.get(position));
        }

        /*
         * (non-Javadoc)
         *
         * @see
         * android.support.v4.view.PagerAdapter#instantiateItem(android.view
         * .ViewGroup, int)
         */
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            container.addView(mList.get(position));
            return mList.get(position);
        }

    }
}
